package com.ruoyi.bigScreen;

import com.ruoyi.bigScreen.vo.AppLevelVO;
import com.ruoyi.bigScreen.vo.AppLevelDataDTO;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.zzw.TAppManage;
import com.ruoyi.common.core.domain.zzw.TRegionInfo;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.service.ITAppManageService;
import com.ruoyi.system.service.ITRegionInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 应用Controller
 *
 * @author xbx
 * @date 2023-10-20
 */
@Api(tags = "应用管理")
@RestController
@RequestMapping("/screen/app")
public class ScreenAppManageController extends BaseController {
    @Autowired
    private ITAppManageService tAppManageService;
    @Autowired
    private ITRegionInfoService regionInfoService;

    @ApiOperation("根据地区查询应用列表")
    @GetMapping("/listByRegion")
    public AjaxResult listByRegion(String districtId) {

        List<TAppManage> list = tAppManageService.listByDistrict(districtId);
        return AjaxResult.success(list);
    }

    @ApiOperation("根据地区等级查询应用列表")
    @GetMapping("/listByLevel")
    public AjaxResult listByLevel() {
        List<TAppManage> province = new ArrayList<>();
        List<TAppManage> city = new ArrayList<>();
        List<TAppManage> district = new ArrayList<>();
        List<TAppManage> central=new ArrayList<>();

        List<TAppManage> list = tAppManageService.list();
        if (CollectionUtils.isNotEmpty(list)) {
            List<TRegionInfo> regionInfoList = regionInfoService.list();
            Map<Long, TRegionInfo> dMap = regionInfoList.stream().collect(Collectors.toMap(TRegionInfo::getCRegionId, Function.identity()));
            for (TAppManage appManage : list) {
                AppLevelDataDTO listVO = new AppLevelDataDTO();
                BeanUtils.copyProperties(appManage, listVO);
                TRegionInfo info = dMap.get(Long.parseLong(appManage.getcAppDistrictId()));
                String areaCode = info.getCAreaCode();
                listVO.setAreaCode(areaCode);
                listVO.setLongitude(info.getCLongitude());
                listVO.setLatitude(info.getCLatitude());
                if (areaCode.endsWith("00000")){
                    central.add(listVO);
                } else if (areaCode.endsWith("0000") ) {
                    province.add(listVO);
                } else if (areaCode.endsWith("00") ) {
                    city.add(listVO);
                }  else {
                    district.add(listVO);
                }
            }
        }
        AppLevelVO vo = new AppLevelVO();
        vo.setProvince(province);
        vo.setCity(city);
        vo.setDistrict(district);
        vo.setCentral(central);
        return AjaxResult.success(vo);
    }

}
